package com.mj.controller;

import com.alibaba.fastjson.JSONObject;
import com.mj.entity.Book;
import org.springframework.aop.target.LazyInitTargetSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class Test {
    @RequestMapping("/json")
    @ResponseBody
    public Object up() {
        return new String[]{"java", "c++", "html"};
    }

    @RequestMapping("/bbk")
    @ResponseBody
    public Book bk() {
        Book b = new Book();
        b.setId(1);
        b.setName("第一名");
        b.setPrice(100);
        b.setDate(new Date());
        return b;
    }

    @RequestMapping("/bks")
    @ResponseBody
    public List<Book> bbks() {
        List<Book> bl = new ArrayList<Book>();
        Book b = new Book();
        b.setId(1);
        b.setName("第一名");
        b.setPrice(100);
        b.setDate(new Date());
        Book bb = new Book();
        bb.setId(1);
        bb.setName("第一名");
        bb.setPrice(100);
        bb.setDate(new Date());
        bl.add(b);
        bl.add(bb);
        return bl;
    }

    //下面是使用了阿里巴巴json的结果
    @RequestMapping("/bkss")
    @ResponseBody
    public String bbkss() {
        List<Book> bl = new ArrayList<Book>();
        Book b = new Book();
        b.setId(1);
        b.setName("第一名");
        b.setPrice(100);
        b.setDate(new Date());
        Book bb = new Book();
        bb.setId(1);
        bb.setName("第一名");
        bb.setPrice(100);
        bb.setDate(new Date());
        bl.add(b);
        bl.add(bb);
        return JSONObject.toJSONString(bl);
    }

    @RequestMapping("/dde")
    @ResponseBody
    public String sn() {
        return "chinese中国人";
    }

    @RequestMapping(value = "/timer", method = RequestMethod.GET)
    @ResponseBody
    public String timer() {
        System.out.println(456);
        return new SimpleDateFormat("yy-MM-dd hh:mm:ss").format(new Date());
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/show")
    @ResponseBody
    public String show() {
        return "hellw-show";
    }

    @RequestMapping("/down.do")
    public ResponseEntity<byte[]> downlo(String fn, HttpServletRequest request) throws IOException {
        String filename = fn;
        String downname = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) + filename.substring(filename.lastIndexOf(".")).toLowerCase();
        String path = request.getServletContext().getRealPath("/download/");//可以将下载路径更换到webif下面，新建一个文件夹作为下载路径，这样可以避免
                                                                              //普通访问人员猜测到下载路径，而webif下的东西是不能直接访问的，即使猜出也不可访问
        InputStream is = new FileInputStream(path + filename);
        byte[] b = new byte[is.available()];//这里和download里面相比，不用循环访问，直接全部读取
        is.read(b);
        HttpHeaders headers = new HttpHeaders();//这里也不是获取了，而是直接new一个
        headers.add("Content-Disposition", "attchement;filename=" + downname);
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(b, headers, HttpStatus.OK);
        return entity;
    }
}
